#include "ShadowMapFrameBuffer.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>

namespace OCPP
{
	ShadowMapFrameBuffer::ShadowMapFrameBuffer(int width,int height)
	{
		ShadowMapFrameBuffer::width = width;
		ShadowMapFrameBuffer::height = height;

		// Framebuffer for Shadow Map
		glGenFramebuffers(1, &ID);

		// Texture for Shadow Map FBO
		
		glGenTextures(1, &shadowMap);
		glBindTexture(GL_TEXTURE_2D, shadowMap);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
		// Prevents darkness outside the frustrum
		float clampColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, clampColor);

		glBindFramebuffer(GL_FRAMEBUFFER, ID);
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap, 0);
		// Needed since we don't touch the color buffer
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
	}

	ShadowMapFrameBuffer::~ShadowMapFrameBuffer()
	{

	}

	void ShadowMapFrameBuffer::bind()
	{
		glViewport(0, 0, width, height);
		glBindFramebuffer(GL_FRAMEBUFFER, ID);
	}

	void ShadowMapFrameBuffer::unbind()
	{
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
	}

	void ShadowMapFrameBuffer::draw()
	{

	}
}